<%@page import="java.awt.print.PageFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" import="org.windblog.core.entity.*,java.util.*,org.windblog.core.*"  pageEncoding="UTF-8"%>
<%!
	String printSpace(int num)
	{
		StringBuilder sb = new StringBuilder("");
		for(int i=0;i<num;i++)
		{
			sb.append("&nbsp;&nbsp;&nbsp;");
		}
		return sb.toString();
	}
%>
<%
	String pagekey = request.getParameter("page");
	Page curPage = null;
	if(pagekey!=null)
	{
		curPage = PageService.getPage(pagekey);
	}
	Page parent = PageService.getParentPage(curPage);
%>
		<jsp:include page="header.jsp"></jsp:include>
		<jsp:include page="sidebar.jsp"></jsp:include>
		<script type="text/javascript">
			$(function(){
				// Accordion
				$("#accordion").accordion("activate",3);
				$("#pages_new").prepend(">");
				$("#update").button();
			});
		</script>

		<div id="content">
			<h2>Edit Page</h2>
			<hr/>
			<form action="setpage" method="post">
			<input type="hidden" name="page" value="<%=curPage.getStrKey() %>"/>
			<div id="updatepanel" class="ui-widget ui-widget-content ui-corner-all" style="float:right;margin-top:10px;padding:0;width:160px;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Publish</h3>
				<input id="update" value="Update" type="submit" />
			</div>
			
			<div id="categorypanel" class="ui-widget ui-widget-content ui-corner-all" style="clear:right;float:right;margin-top:10px;width:160px;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Attributes</h3>
				<div style="padding:0 1em 1em 1em;">
				<h5>Parent</h5>
				<label class="hidden" for="parent_id">Page Parent</label>
				<select name="parent" id="parent_id">
					<option value="">Main Page (no parent)</option>
					<%
					Page root = PageService.getRoot();
					Stack<Page> pageStack = new Stack<Page>();
					Stack<Integer> levelStack = new Stack<Integer>();
					int curLevel = -1;
					pageStack.push(root);
					levelStack.push(curLevel);
					if(root!=null)
					{
						while(!pageStack.empty())
						{
							Page currentPage = pageStack.pop();
							curLevel = levelStack.pop();
							if(!currentPage.isRoot()){
							%>
							<option class="level-<%=curLevel %>" value="<%=currentPage.getStrKey() %>" <%=currentPage.equals(parent)?"selected":"" %>><%=printSpace(curLevel)+currentPage.getTitle() %></option>											
							<%}
							List<Page> childPages = new LinkedList<Page>(currentPage.getChildPages());
							Collections.sort(childPages);
							for(Page p : childPages){
								pageStack.push(p);
								levelStack.push(curLevel+1);
							}
							
						}//end while
					}//end if
					%>
				</select>
				<br/>
				<p>You can arrange your pages in hierarchies, for example you could have an “About” page that has “Life Story” and “My Dog” pages under it. There are no limits to how deeply nested you can make pages.</p>
				<hr/>
				<h5>Order</h5>
				<p><label class="hidden" for="menu_order">Page Order</label><input name="order" type="text" size="4" id="order" value="<%=curPage.getPriority() %>" /></p>
				<p>Pages are usually ordered alphabetically, but you can put a number above to change the order pages appear in. (We know this is a little janky, it&#8217;ll be better in future releases.)</p>
				<hr/>
				</div>
			</div>
			
			<input id="title" name="title" type="text" value="<%=curPage.getTitle() %>"/>
			
			<textarea id="ed3" name="content" rows="30" cols="80" style="width: 80%">
			<%=curPage.getContent() %>
			</textarea>
			
			</form>
						
		</div>
		
		<jsp:include page="footer.jsp"></jsp:include>
